package com.example.dell.spinnertest_224lx;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowInsets;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   private TextView tvinfo;
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        //定义数据源----1
        String[]cities={"北京","上海","天津","大连"};
        //定义适配器---2
        ArrayAdapter<String> adapter=new  ArrayAdapter<String>(this,//上下文
                android.R.layout.simple_spinner_item, //条目布局
                cities //数据源
                );
        //设置spinner下拉按钮样式---3
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        //spinner与adapter---4
        spinner.setAdapter(adapter);
        spinner.setPrompt("请选择城市");
        ////Spinner点击条目事件---5
       spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
           @Override
           public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
               tvinfo.setText("您选择的是："+parent.getItemAtPosition(position).toString());
           }

           @Override
           public void onNothingSelected(AdapterView<?> parent) {
                   //不操作
           }
       });

    }
    private void init(){
        tvinfo=(TextView)findViewById(R.id.textView);
        spinner=(Spinner)findViewById(R.id.spinner);
    }
}
